package com.syx.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syx.entity.Car;
import com.syx.entity.Users;
import com.syx.service.CarService;
import com.syx.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CarController {
    @Resource
    private CarService carService;

    //列表
    @RequestMapping("list.do")
    public String carList(Model model,
                          String cname,
                          String ontimeMin,
                          String ontimeMax,
                          String tid,
                          @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum){
        PageHelper.startPage(pageNum,3);
        Map map = new HashMap();
        map.put("cname",cname);
        map.put("ontimeMin",ontimeMin);
        map.put("ontimeMax",ontimeMax);
        map.put("tid",tid);
        List list = carService.carList(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    //动态下拉品牌
    @RequestMapping("typeList.do")
    @ResponseBody
    public Object typeList(){
        List list = carService.typeList();
        return list;
    }

    @RequestMapping("addTo.do")
    public String addTo(){
        return "add";
    }


    @RequestMapping("upd.do")
    public String updTo(){
        return "update";
    }

    //动态下拉城市
    @RequestMapping("cityList.do")
    @ResponseBody
    public Object cityList(Integer pid){
        List list = carService.cityList(pid);
        return list;
    }

    //动态复选颜色
    @RequestMapping("colorList.do")
    @ResponseBody
    public Object colorList(){
        List list = carService.colorList();
        return list;
    }

    //信息回显
    @RequestMapping("hx.do")
    @ResponseBody
    public Object hx(Integer cid){
        Map map = carService.hx(cid);
        return map;
    }

    //添加
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Car car, HttpSession httpSession, MultipartFile photo,Integer[] coids){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(httpSession, photo, "upload");
            car.setPic(upload);
        }
        int i = carService.add(car,coids);
        return i;
    }

    //删除
    @RequestMapping("del.do")
    @ResponseBody
    public Object del(String  cid){
        System.out.println(cid);
       int i =  carService.del(cid);
        return i;
    }

    @RequestMapping("update.do")
    @ResponseBody
    public Object update(Car car, HttpSession httpSession, MultipartFile photo,Integer[] coids){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(httpSession, photo, "upload");
            car.setPic(upload);
        }
        int i = carService.update(car,coids);
        return i;
    }

    @RequestMapping("login.do")
    public String login(Users users, HttpSession httpSession){
        Users user = carService.login(users);
        if (user !=null){
            httpSession.setAttribute("user",user);
            return "redirect:list.do";
        }
        return "redirect:index.jsp";
    }

}
